# -*- coding: utf-8 -*-
#__auth__ = "cpr"
#__date__ = "2018.03.14"
from __future__ import unicode_literals

from django.shortcuts import render
from django.db import connections,transaction
from django.shortcuts import render,HttpResponse,render_to_response,redirect
from django.http import JsonResponse
from merchant import Merchant
from manager import MerchantManager
from mauth import VerfUser
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import login_required
from django.contrib import auth
from django.contrib.auth.models import User, Group, Permission
from onlineshop import Shop
from report import DataReport
from finance import Finance
from  platform import WechatPlatform
import os 
import json

ret = {"status":0,"errInfo":""}


def index(requests,args):
    print ret  
    return HttpResponse(json.dumps(ret))

def merchant(requests,args):
    """ 商户查询 """
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    try:
        cmd = cfgParam["cmd"]
    except Exception,e:
        print e
        ret = {'status':0,'info_code':10001,'info':'cmd error.'}
        return JsonResponse(ret)
         
    ob = Merchant(cfgParam)
    ret = getattr(ob,cmd)()
    return JsonResponse(ret)

def login(requests,args):
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    ret = {'status':0,'info_code':10000} 
    user = auth.authenticate(username=cfgParam["username"], password=cfgParam["password"]) 
    if user is not None and user.is_active:
        auth.login(requests, user)
	ret['status']      = 1
        ret['permissions'] =  list(user.get_all_permissions())
	
    else:
        ret['info_code'] = 10001
    return JsonResponse(ret)

def mlogin(requests,args):
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    try:
        cmd = cfgParam["cmd"]
    except Exception,e:
        print e
        ret = {'status':0,'info_code':10001,'info':'cmd error.'}
    ob = VerfUser(cfgParam)
    ret = getattr(ob,cmd)()
    return JsonResponse(ret)

def manager(requests,args):
    print 'manag'
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    try:
        cmd = cfgParam["cmd"]
    except Exception,e:
        print e
        ret = {'status':0,'info_code':10001,'info':'cmd error.'}
    ob = MerchantManager(cfgParam,requests)
    ret = getattr(ob,cmd)()
    print ret

    return JsonResponse(ret)

def shop(requests,args):
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    try:
        cmd = cfgParam["cmd"]
    except Exception,e:
        print e
        ret = {'status':0,'info_code':10001,'info':'cmd error.'}
    ob = Shop(cfgParam)
    ret = getattr(ob,cmd)()
    return JsonResponse(ret)
    


def report(requests,args):
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    try:
        cmd = cfgParam["cmd"]
    except Exception,e:
        print e
        ret = {'status':0,'info_code':10001,'info':'cmd error.'}
    ob = DataReport(cfgParam)
    ret = getattr(ob,cmd)()
    return JsonResponse(ret)


def finance(requests,args):
    print 'finance ------'
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    try:
        cmd = cfgParam["cmd"]
    except Exception,e:
        print e
        ret = {'status':0,'info_code':10001,'info':'cmd error.'}
    ob = Finance(cfgParam)
    ret = getattr(ob,cmd)()
    print ret
    return JsonResponse(ret)

def wechat(requests,args):
    print 'wechat ------'
    if requests.GET:
        cfgParam = requests.GET
    if requests.POST:
	cfgParam = requests.POST
    try:
        cmd = cfgParam["cmd"]
    except Exception,e:
        print e
        ret = {'status':0,'info_code':10001,'info':'cmd error.'}
    ob = WechatPlatform(cfgParam)
    ret = getattr(ob,cmd)()
    return JsonResponse(ret)


